package com.ask.forMe.model.entity;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class UserContext {
    private Long userId;
    private String username;
    private Set<String> roles; // 用户的身份（可以有多个）
    private Set<String> permissions; // 用户拥有的权限
    private Map<String, Object> attributes = new HashMap<>(); // 用户属性

    // 权限检查方法
    public boolean hasPermission(String permission) {
        return permissions.contains(permission) ||
                roles.contains("ADMIN"); // 管理员拥有所有权限
    }

    // 资源权限检查
    public boolean hasResourcePermission(String action, String resourceType, String resourceId) {
        String permissionKey = action + ":" + resourceType;
        return hasPermission(permissionKey) ||
                hasPermission(permissionKey + ":" + resourceId);
    }
}

